<template>
  <div>

  </div>  
</template>

<script lang="ts">
import { defineComponent, onMounted, onUnmounted, watch, reactive, computed, ref } from 'vue';
import { EnumWebsocketState, socket } from 'src/socket/websocket';
import { EnumWSCommand, useScadaPacket } from 'src/hook/editor/useScadaPacket';
import Timeout = NodeJS.Timeout;

export default defineComponent({
  name: 'DeviceDataAccept',   
  setup (props, context) {
    //*********************************************************
    // 变量表
    //*********************************************************
    //连接状态
    let wsConnected = ref(false);
    let wsTimer: Timeout;

    //socket通信函数
    const {
      WS_FRAME_HEADER,
      wsCertifyUser,
      wsRegisterVariable,
      wsReadVariable
    } = useScadaPacket();

    //web socket连接成功
    const onWebsocketConnected = () => {
      //连接状态
      wsConnected.value = true;

      //提示
      showMessage('Websocket服务器连接成功！');

      //认证
      wsCertifyUser(socket, user.value.username,
        user.value.password, application.value.projectId);
    }

    //web socket接收函数，msg为ArrayBuffer
    const onWebsocketReceived = (msg: any) => {
      //连接状态
      wsConnected.value = true;

      //二进制ArrayBuffer类型接收数据
      let len = msg.byteLength;
      let uint8Arr = new Uint8Array(msg);

      //解析
      let vid = 0, vdata = 0, status = 0;
      let ofs = 0;
      let header = (uint8Arr[ofs++] << 8) | uint8Arr[ofs++];
      let command = (uint8Arr[ofs++] << 8) | uint8Arr[ofs++];
      let size = (uint8Arr[ofs++] << 8) | uint8Arr[ofs++];

      //帧格式判断
      if ((header !== WS_FRAME_HEADER) || (len !== size + 6)) {
        console.log('数据帧格式不正确！');
        return;
      }

      //帧解析
      switch (command) {
        case EnumWSCommand.WS_CERTIFY_USER:
          if (size === 2) {
            status = (uint8Arr[ofs++] << 8) | uint8Arr[ofs++];
            if (status === 1) {
              wsRegisterVariable(socket, varTbl.value);
            }
          }
          break;
        case EnumWSCommand.WS_REGISTER_VARIABLE:
          if (size === 2) {
            status = (uint8Arr[ofs++] << 8) | uint8Arr[ofs++];
            if (status === 1) {
              wsReadVariable(socket, varTbl.value);
            }
          }
          break;
        case EnumWSCommand.WS_READ_VARIABLE:
          if (size % 6 === 0) {
            let num = size % 6;
            for (let i = 0; i < num; i++) {
              vid = (uint8Arr[ofs++] << 24) | (uint8Arr[ofs++] << 16) | (uint8Arr[ofs++] << 8) | uint8Arr[ofs++];
              vdata = (uint8Arr[ofs++] << 8) | uint8Arr[ofs++];
              commonStore.addVariableTbl(vid, vdata);
            }
          }
          break;
        default:
          break;
      }
    }

    //关闭
    const onWebsocketClosed = () => {
      wsConnected.value = false;
    }

    //开启websocket
    const connectWebsocketServer = () => {
      //设置websocket通信信息
      //socket.connectURL = 'ws://' + canvas.value.wsAddr + ':' + canvas.value.wsPort;

      //初始化
      socket.init(onWebsocketReceived, onWebsocketConnected, onWebsocketClosed);

      //开始连接
      socket.connect('ws://127.0.0.1:6678');
    }

    //关闭websocket
    const closeWebsocket = () => {
      socket.close();
      wsConnected.value = false;
    }

    const init = () => {
      debugger
      console.log(dataSocket)
      
    }

    onMounted(() => {
      init();
    });
    
    return { 
      dataSocket  
    }
  },
})
</script>
